package org.light.generator;

import java.util.ArrayList;
import java.util.List;

import org.light.core.Writeable;
import org.light.domain.Statement;
import org.light.exception.ValidateException;
import org.light.utils.StringUtil;
import org.light.utils.WriteableUtil;

public class ConfigYamlGenerator {
	protected String fileName = "config.yaml";
	protected String dbUserName = "root";
	protected String dbName = "";
	protected String dbPassword = "";
	protected String dbType = "MariaDB";
	
	public ConfigYamlGenerator(String dbName,String dbUserName,String dbPassword, String dbType){
		super();
		this.fileName = "config.yaml";
		this.dbName = dbName;
		this.dbUserName = dbUserName;
		this.dbPassword = dbPassword;
		this.dbType = dbType;
	}

	public String generateString() throws ValidateException{
		List<Writeable> sList = new ArrayList<Writeable>();
		if (StringUtil.isBlank(this.getDbType())||"MariaDB".equalsIgnoreCase(this.getDbType())||"MariaDB".equalsIgnoreCase(this.getDbType())||"MySQL".equalsIgnoreCase(this.getDbType())) {
			sList.add(new Statement(1000L,0,"mysql:"));
			sList.add(new Statement(2000L,0,"  max_idle_conns: 50"));
			sList.add(new Statement(3000L,0,"  source_name: "+this.dbUserName+":"+this.dbPassword+"@tcp(127.0.0.1:3306)/"+this.dbName+"?parseTime=true&charset=utf8&loc=Local"));
			sList.add(new Statement(4000L,0,"addr: 127.0.0.1:8081"));
		}else if ("PostgreSQL".equalsIgnoreCase(this.getDbType())||"pgsql".equalsIgnoreCase(this.getDbType())) {
			sList.add(new Statement(1000L,0,"postgresql:"));
			sList.add(new Statement(2000L,0,"  max_idle_conns: 50"));
			sList.add(new Statement(3000L,0,"  source_name: host=localhost port=5432 user="+this.dbUserName+" password="+this.dbPassword+" dbname="+this.dbName+" sslmode=disable"));
			sList.add(new Statement(4000L,0,"addr: 127.0.0.1:8081"));
		}else if ("Oracle".equalsIgnoreCase(this.getDbType())) {
			sList.add(new Statement(1000L,0,"oracle:"));
			sList.add(new Statement(2000L,0,"  max_idle_conns: 50"));
			sList.add(new Statement(3000L,0,"  source_name: "+this.dbUserName+"/"+this.dbPassword+"@localhost:1521/"+this.dbName));
			sList.add(new Statement(4000L,0,"addr: 127.0.0.1:8081"));
		}else {
			throw new ValidateException(this.getDbType()+"数据库不被支持！");
		}
		
		return WriteableUtil.merge(sList).getContent();
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getDbUserName() {
		return dbUserName;
	}

	public void setDbUserName(String dbUserName) {
		this.dbUserName = dbUserName;
	}

	public String getDbName() {
		return dbName;
	}

	public void setDbName(String dbName) {
		this.dbName = dbName;
	}

	public String getDbPassword() {
		return dbPassword;
	}

	public void setDbPassword(String dbPassword) {
		this.dbPassword = dbPassword;
	}

	public String getDbType() {
		return dbType;
	}

	public void setDbType(String dbType) {
		this.dbType = dbType;
	}

}
